package com.rxkj.controller;

import com.rxkj.authen.RoleLevel;
import com.rxkj.entity.sys.NavMenu;
import com.rxkj.entity.sys.NavMenuVo;
import com.rxkj.service.NavMenuService;
import com.rxkj.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/sys")
@Api(tags = "导航菜单")
public class NavMenuController {

    private static Logger logger = LoggerFactory.getLogger(NavMenuController.class);

    @Autowired
    private NavMenuService navMenuService;

    @PostMapping("/getNavMenu")
    @ApiOperation("获取所有导航菜单信息")
//    @RoleLevel(2)
    public ResponseResult getNavMenus(Integer id, String userName) {
        List<NavMenu> menus = navMenuService.getNavMenus(id, userName);
        return menus.size() > 0 ? ResponseResult.success("查询成功", menus) : ResponseResult.error("查询失败");
    }

    @PostMapping("/getNavMenus")
    @ApiOperation("根据用户权限获取导航菜单")
    @RoleLevel(2)
    public ResponseResult getNavMenuss(HttpServletRequest request) {
        Integer id = (Integer) request.getSession().getAttribute("id");
        String userName = (String) request.getSession().getAttribute("userName");
        List<NavMenu> menus = navMenuService.getNavMenu(id, userName);
        return menus.size() > 0 ? ResponseResult.success("查询成功", menus) : ResponseResult.error("查询失败");
    }

    @PostMapping("/getTreeMenus")
    @ApiOperation("获取所有菜单权限信息")
//    @RoleLevel(2)
    public ResponseResult getTreeMenus() {
        List<NavMenu> menus = navMenuService.getTreeMenus();
        return menus.size() > 0 ? ResponseResult.success("查询成功", menus) : ResponseResult.error("未查询相关信息");
    }

    @PostMapping("/getRoleMenus")
    @ApiOperation("根据角色信息获取权限")
//    @RoleLevel(2)
    public ResponseResult getRoleMenus(Integer id, String roleName) {
        List<NavMenu> menus = navMenuService.getRoleMenus(id, roleName);
        return menus.size() > 0 ? ResponseResult.success("查询成功", menus) : ResponseResult.error("未查询相关信息");
    }

    @PostMapping("/addRoleMenus")
    @ApiOperation("添加权限")
//    @RoleLevel(2)
    public ResponseResult addRoleMenus(Integer id, Integer[] authority) {
        try {
            navMenuService.addRoleMenus(id, authority);
            return ResponseResult.success();
        } catch (Exception e) {
            return ResponseResult.error();
        }
    }

    @PostMapping("/getAllMenus")
    @ApiOperation("获取所有菜单信息")
//    @RoleLevel(2)
    public ResponseResult getAllMenus() {
        List<NavMenuVo> menus = navMenuService.getAllMenus();
        return menus.size() > 0 ? ResponseResult.success("查询成功", menus) : ResponseResult.error("未查询相关信息");
    }

    @PostMapping("/AddMenus")
    @ApiOperation("添加菜单")
//    @RoleLevel(2)
    public ResponseResult AddMenus(@RequestBody NavMenu navMenu) {
        int info = navMenuService.AddMenus(navMenu);
        if (info == 0) {
            return ResponseResult.error("该路径已存在");
        } else if (info > 0) {
            return ResponseResult.success();
        }else {
            return ResponseResult.error();
        }
    }

    @GetMapping("/DeleteMenu")
    @ApiOperation("删除菜单")
//    @RoleLevel(2)
    public ResponseResult DeleteMenu(Integer id) {
        try {
            navMenuService.DeleteMenu(id);
            return ResponseResult.success();
        } catch (Exception e) {
            return ResponseResult.error();
        }
    }

    @PostMapping("/EditMenu")
    @ApiOperation("编辑菜单")
//    @RoleLevel(2)
    public ResponseResult EditMenu(@RequestBody NavMenu navMenu) {
        int info = navMenuService.EditMenu(navMenu);
        return info > 0 ? ResponseResult.success() : ResponseResult.error("修改失败");
    }

    @GetMapping("/getMenuId")
    @ApiOperation("根据id查询菜单信息")
//    @RoleLevel(2)
    public ResponseResult getMenuId(Integer id) {
        NavMenu info = navMenuService.getMenuId(id);
        return info !=null ? ResponseResult.success(info) : ResponseResult.error("查询失败");
    }

}
